<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre;
        height: 200px;
        overflow: auto;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../three.js-r151/build/three.module.js",
		"three/addons/": "../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">

    </div>

    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

        // 1. 创建三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        // MeshBasicMaterial MeshLambertMaterial MeshPhongMaterial
        const material = new THREE.MeshPhongMaterial({
            color: 0x00ffff, //设置材质颜色
            transparent: false,//开启透明
            opacity: 0.5,//设置透明度
        });

        const mesh = new THREE.Mesh(geometry, material);
        scene.add(mesh)

        const mesh2 = new THREE.Mesh(geometry, material);
        mesh2.position.set(0, 0, 2)
        scene.add(mesh2)

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(10);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 50);
        camera.position.set(9, 2, 5)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer({
            antialias: true,
        });
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 设置相机控件轨道控制器OrbitControls
        const controls = new OrbitControls(camera, canvasDom);
        // 如果OrbitControls改变了相机参数，重新调用渲染器渲染三维场景
        controls.addEventListener('change', function () {
            renderer.render(scene, camera); //执行渲染操作
        });//监听鼠标、键盘事件


        // 2. 打光 PointLight DirectionalLight HemisphereLight
        {
            const light = new THREE.PointLight(0xffffff, 1.0);
            // const light = new THREE.HemisphereLight(0xffffff, 0xccccc, 1.0);
            // const light = new THREE.AmbientLight (0xffffff, 1.0);
            light.position.set(6, 0, 0);
            // scene.add(light);
        }

        // 切换为聚光灯
        {
            // 聚光源
            // 0xffffff:光源颜色
            // 1.0：光照强度intensity
            const spotLight = new THREE.SpotLight(0xffffff, 1.0);

            // 设置聚光光源发散角度
            spotLight.angle = Math.PI / 10;//光锥角度的二分之一
            // 设置聚光光源位置
            spotLight.position.set(6, 0, 0);
            // 一个对象，默认指向坐标原点
            console.log('聚广源指向目标', spotLight.target);
            // spotLight.target是一个模型对象Object3D，默认在坐标原点
            spotLight.target.position.set(0, 0, 2);
            //spotLight.target添加到场景中.target.position才会起作用
            scene.add(spotLight.target);

            scene.add(spotLight);//光源添加到场景中

            // 聚广源辅助对象，可视化聚广源
            const spotLightHelper = new THREE.SpotLightHelper(spotLight, 0xffffff)
            scene.add(spotLightHelper);

        }


        renderer.render(scene, camera);

    </script>
</body>

</html>